package com.edu.dao.impl;

import com.edu.dao.CommentDao;
import com.edu.entity.CommentEntity;
import com.edu.utils.DatabaseManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;

/**
 * com.edu.dao.impl
 * 2024/7/14 14:55
 *
 * @author Fancy
 * @version 1.0
 */
public class CommentDaoImpl implements CommentDao {
    private Connection connection;

    public CommentDaoImpl() {
        try {
            connection = DatabaseManager.getInstance().getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int addComment(CommentEntity comment) {
        try {
            String sql = "INSERT INTO Comment (user_id, book_id, content, rating) value ( ?, ?, ?, ?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, comment.getUserId());
            preparedStatement.setInt(2, comment.getBookId());
            preparedStatement.setString(3, comment.getContent());
            preparedStatement.setInt(4, comment.getRating());
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteReview(int reviewId) {
        try {
            String sql = "DELETE FROM Comment WHERE comment_id = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, reviewId);
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public CommentEntity getCommentById(int reviewId) {
        try {
            String sql = "SELECT * FROM Comment WHERE comment_id = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, reviewId);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                return new CommentEntity(
                        resultSet.getInt("comment_id"),
                        resultSet.getInt("user_id"),
                        resultSet.getInt("book_id"),
                        resultSet.getString("content"),
                        resultSet.getInt("rating"),
                        resultSet.getTimestamp("comment_date")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public LinkedList<CommentEntity> getBookReviewsByBookId(int bookId) {
        LinkedList<CommentEntity> commentEntities = new LinkedList<>();
        try {
            String sql = " SELECT * "
                    + "FROM Comment AS c "
                    + "JOIN User AS u ON c.user_id = u.user_id "
                    + "WHERE c.book_id = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, bookId);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                commentEntities.add(new CommentEntity(
                                resultSet.getInt("comment_id"),
                                resultSet.getInt("user_id"),
                                resultSet.getInt("book_id"),
                                resultSet.getString("content"),
                                resultSet.getInt("rating"),
                                resultSet.getTimestamp("comment_date"),
                                resultSet.getString("username")
                        )
                );
            }
            return commentEntities;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
}
